package com.bungarus.busi;

import com.bungarus.codec.protobuf.MessageProtobuf;
import com.bungarus.model.MessageBuilder;
import com.bungarus.model.MessageType;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.apache.log4j.Logger;

/**
 * Created by tang on 2018/11/25.
 */
public class ProcessMessageHandler extends ChannelInboundHandlerAdapter {
    Logger logger = Logger.getLogger(ProcessMessageHandler.class);
    StatusContainer container = StatusContainer.getInstance();
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if(msg instanceof MessageProtobuf.Message){
            MessageProtobuf.Message message = (MessageProtobuf.Message) msg;
            if(MessageType.HEARTBEAT_PING == message.getHeader().getType()){
                //send back pong
                ctx.writeAndFlush(MessageBuilder.buildHeartbeatPongMessage(message.getHeader().getId()));
                logger.info("header of session or status messages from access layer: " + message.getHeader().toString());
                logger.info("body of session or status messages form access layer: " + message.getBody().toString());
            }
            else if(MessageType.AUTH == message.getHeader().getType()){
                //TODO: maintain access session info
                //1. update the user's session in session container
                //2. update the user's status ('online') in status container and broadcast the status to everyone who is
                // in the same group with him.
            }
            else if(MessageType.STATUS == message.getHeader().getType()) {
                //TODO: maintain user online status
                //1. update the user's status in container
                //2. send the user's latest status to everyone who is in the same group with him.
                try {
                    container.put(message.getHeader().getTenantID() + message.getHeader().getFrom(),
                            Integer.valueOf(message.getHeader().getTo()));
                }
                catch (StatusContainerException e) {
                    e.printStackTrace();
                }
            }
            else if(MessageType.IM == message.getHeader().getType()) {
                //TODO: send the im message to the receipt
                //1. find receipt in current cluster first.
                //2. find receipt in other clusters second if the person cannot be found in step 1.
                //3. mark the message as offline message if the person cannot be found in step 2.
                //4. save the offline message.
            }
            else {
                //TODO: send the other type message. it is not necessary to mark the message as offline if
                // the message cannot be sent.
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
    }
}
